﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using System.Data;
using Telerik.Web.UI;
using System.Configuration;
using System.Globalization;
namespace Web.Quote
{
    public partial class AddNewQuote : System.Web.UI.Page
    {
        static string prevPage = String.Empty;
        static long? assignedToID = null;
        static USER user;
        static int rowIndex = 0;
        static ROLE r;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            assignedToID = null;
            user = (USER)Session["User"];
            if (user != null)
            {
                if (Request.UrlReferrer != null)
                {
                    prevPage = Request.UrlReferrer.ToString();
                    hdntxtPrevPage.Value = prevPage;
                }
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                {
                    assignedToID = user.ID;
                    divAssignedTo.Visible = false;
                }
                if (ViewState["Products"] == null)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add(new DataColumn("ID"));
                    dt.Columns.Add(new DataColumn("ProductID"));
                    dt.Columns.Add(new DataColumn("ProductName"));
                    dt.Columns.Add(new DataColumn("ProductDescription"));
                    dt.Columns.Add(new DataColumn("GST", typeof(System.Double)));
                    dt.Columns.Add(new DataColumn("Quantity", typeof(System.Double)));
                    dt.Columns.Add(new DataColumn("Image"));
                    dt.Columns.Add(new DataColumn("UnitPrice", typeof(System.Double)));
                    dt.Columns.Add(new DataColumn("DiscountPrice", typeof(System.Double)));
                    dt.Columns.Add(new DataColumn("DiscountPercent", typeof(System.Double)));
                    dt.Columns.Add(new DataColumn("TotalPrice", typeof(System.Double)));
                    dt.Columns.Add(new DataColumn("DiscountType"));
                    ViewState["Products"] = AddRow(dt);
                }
                LoadQuoteStage();
                LoadAllUser();
            }
        }
        private DataTable AddRow(DataTable dt)
        {
            DataRow dr = dt.NewRow();
            {
                dr["ID"] = rowIndex;
                dr["ProductID"] = -1;
                dr["ProductDescription"] = "";
                dr["GST"] = 0;
                dr["ProductName"] = "";
                dr["Quantity"] = 0;
                dr["Image"] = "../ProductImages/no_image_for_this_product.gif";
                dr["UnitPrice"] = 0;
                dr["DiscountPrice"] = 0;
                dr["DiscountPercent"] = 0;
                dr["DiscountType"] = "Per";
                dr["TotalPrice"] = 0;
                dt.Rows.Add(dr);
                rowIndex++;
            }
           
            return dt;
        }
        private void LoadAllUser()
        {
            ddlAssignedTo.Items.Clear();
            List<USER> users = UserService.ListAll();
            if (users != null)
            {
                foreach (USER u in users)
                {
                    ddlAssignedTo.Items.Add(new RadComboBoxItem(u.SUR_NAME + " " + u.NAME, u.ID.ToString()));
                }
            }
        }

        private void LoadQuoteStage()
        {
            ddlQuoteStage.DataSource = QuoteService.GetAllQuoteStage();
            ddlQuoteStage.DataBind();
        }
        private int addNewQuote()
        {
            QUOTE quote = new QUOTE();
            //General information
            DateTime dt = DateTime.Now;
            quote.DATE_ENTERED = dt;
            quote.DATE_MODIFIED = dt;
            //Quote Details
            quote.SUBJECT = txtSubject.Text;
            if (hdntxtConID.Value != "")
                quote.CONTACT_ID = Convert.ToInt64(hdntxtConID.Value);            
            quote.QUOTE_STAGE_ID = Convert.ToInt64(ddlQuoteStage.SelectedValue);
            quote.VALID_UNTIL = dpValidUntil.SelectedDate;
            if (hdntxtOrgID.Value != "")
                quote.ORGANIZATION_ID = Convert.ToInt64(hdntxtOrgID.Value);
            
            if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                quote.ASSIGNED_TO = user.ID;
            else
            {
                if (ddlAssignedTo.SelectedItem == null)
                    quote.ASSIGNED_TO = null;
                else
                    quote.ASSIGNED_TO = Convert.ToInt64(ddlAssignedTo.SelectedValue);
            }
            //Address detail
            quote.OFFICE_ADDRESS = txtOfficeAddress.Text.Trim();
            quote.HOME_ADDRESS = txtHomeAddress.Text.Trim();
            quote.OFFICE_PO_BOX = txtOfficePOBox.Text.Trim();
            quote.HOME_PO_BOX = txtHomePOBox.Text.Trim();
            quote.OFFICE_CITY = txtOfficeCity.Text.Trim();
            quote.HOME_CITY = txtHomeCity.Text.Trim();
            quote.OFFICE_STATE = txtOfficeState.Text.Trim();
            quote.HOME_STATE = txtHomeState.Text.Trim();
            quote.OFFICE_POSTAL_CODE = txtOfficePostalCode.Text.Trim();
            quote.HOME_POSTAL_CODE = txtHomePostalCode.Text.Trim();
            quote.HOME_COUNTRY = txtHomeCountry.Text.Trim();
            quote.OFFICE_COUNTRY = txtOfficeCountry.Text.Trim();
            //Item details
            ITEM item = new ITEM();
            item.DATE_CREATED = dt;
            List<ITEM_DETAIL> itemDetails = GetItemList(ref item);
            return QuoteService.AddNewQuote(quote, item, itemDetails);
        }
        private List<ITEM_DETAIL> GetItemList(ref ITEM i)
        {
            List<ITEM_DETAIL> re = new List<ITEM_DETAIL>();
            RadGrid1.AllowPaging = false;
            RadGrid1.Rebind();
            double? total_price = 0;
            foreach (GridDataItem item in RadGrid1.Items)
            {
                RadNumericTextBox rntxtQuantity = (RadNumericTextBox)item.FindControl("rntxtQuantity");
                RadNumericTextBox rntxtGST = (RadNumericTextBox)item.FindControl("rntxtGST");
                RadNumericTextBox rntxtUnitPrice = (RadNumericTextBox)item.FindControl("rntxtUnitPrice");
                RadNumericTextBox rntxtTotalPrice = (RadNumericTextBox)item.FindControl("rntxtTotalPrice");
                RadNumericTextBox rntxtDiscountPercent = (RadNumericTextBox)item.FindControl("rntxtDiscountPercent");
                RadNumericTextBox rntxtDiscountPrice = (RadNumericTextBox)item.FindControl("rntxtDiscountPrice");
                RadButton rbtnPercent = (RadButton)item.FindControl("rbtnPercent");
                ITEM_DETAIL d = new ITEM_DETAIL();
                d.PRODUCT_ID = Convert.ToInt64(item.GetDataKeyValue("ProductID"));
                d.QUANTITY = rntxtQuantity.Value;
                d.PRICE = rntxtTotalPrice.Value;
                d.DISCOUNT_PERCENT = rbtnPercent.Checked ? rntxtDiscountPercent.Value : 0;
                d.DISCOUNT_PRICE = rbtnPercent.Checked ? 0 : rntxtDiscountPrice.Value;
                d.GST = rntxtGST.Value;
                d.UNIT_PRICE = rntxtUnitPrice.Value;
                total_price += d.PRICE;
                //if (d.PRICE > 0)
                    re.Add(d);
            }
            i.TOTAL_PRICE = total_price;
            RadGrid1.AllowPaging = true;
            RadGrid1.Rebind();
            return re;
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int newQuoteResult = addNewQuote();
            if (newQuoteResult == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Add new quote successful !" + "','" + "Quotes.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Add new quote successful !", "Quotes.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessage('" + "Can not add new quote !" + "');");
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not add new quote !');", true);
        }

        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Products"];
            RadGrid1.DataSource = dt;
        }
        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridPagerItem)
            {
                GridPagerItem oGridPagerItem = (GridPagerItem)e.Item;
                RadComboBox oRadComboBox = (RadComboBox)oGridPagerItem.FindControl("PageSizeComboBox");
                oRadComboBox.Width = Unit.Pixel(50);
            }
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;
                LinkButton lbtnProductSearch = (LinkButton)item.FindControl("lbtnProductSearch");
                LinkButton lbtnProductClear = (LinkButton)item.FindControl("lbtnProductClear");
                lbtnProductSearch.OnClientClick = string.Format("return openProductSearchWindow('{0}')", e.Item.ItemIndexHierarchical);
                //     lbtnProductClear.OnClientClick = string.Format("return clearProductName('{0}')", e.Item.ItemIndexHierarchical);
            }
        }
        protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            GridDataItem item = e.Item as GridDataItem;
            if (e.CommandName == "Delete")
            {
                DataTable dt = (DataTable)ViewState["Products"];
                DataRow dr = null;
                foreach (DataRow r in dt.Rows)
                {
                    if (r["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                    {
                        dr = r;
                        break;
                    }
                }
                if (dr != null)
                    dt.Rows.Remove(dr);

                RadGrid1.DataSource = dt;
                RadGrid1.DataBind();
            }
            if (e.CommandName == "Clear")
            {
                DataTable dt = (DataTable)ViewState["Products"];

                foreach (DataRow r in dt.Rows)
                {
                    if (r["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                    {
                        {
                            r["ProductID"] = -1;
                            r["ProductDescription"] = "";
                            r["GST"] = 0;
                            r["ProductName"] = "";
                            r["Quantity"] = 0;
                            r["Image"] = "../ProductImages/no_image_for_this_product.gif";
                            r["UnitPrice"] = 0;
                            r["DiscountPrice"] = 0;
                            r["DiscountPercent"] = 0;
                            r["DiscountType"] = "Per";
                            r["TotalPrice"] = 0;
                        }


                        break;
                    }
                }

                dt.AcceptChanges();
                ViewState["Products"] = dt;
                RadGrid1.DataSource = dt;
                RadGrid1.DataBind();
            }
        }
        double? sum = 0;
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Products"];
            if (e.Item is GridDataItem)
            {
                GridDataItem item = e.Item as GridDataItem;
                System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)item.FindControl("productImage"); 
                TextBox txtProductDescription = (TextBox)item.FindControl("txtProductDescription");
                RadNumericTextBox rntxtGST = (RadNumericTextBox)item.FindControl("rntxtGST");
                RadNumericTextBox rntxtUnitPrice = (RadNumericTextBox)item.FindControl("rntxtUnitPrice");
                RadButton rbtnPercent = (RadButton)item.FindControl("rbtnPercent");
                RadButton rbtnPrice = (RadButton)item.FindControl("rbtnPrice");
                RadNumericTextBox rntxtQuantity = (RadNumericTextBox)item.FindControl("rntxtQuantity");
                Panel divDiscountPercent = (Panel)item.FindControl("divDiscountPercent");
                Panel divDiscountPrice = (Panel)item.FindControl("divDiscountPrice");
                RadNumericTextBox rntxtDiscountPercent = (RadNumericTextBox)item.FindControl("rntxtDiscountPercent");
                RadNumericTextBox rntxtDiscountPrice = (RadNumericTextBox)item.FindControl("rntxtDiscountPrice");
                RadNumericTextBox rntxtTotalPrice = (RadNumericTextBox)item.FindControl("rntxtTotalPrice");
                /////////////////////////
                rntxtQuantity.Attributes.Add("onclick", "Select('" + e.Item.ItemIndex + "');");
                rntxtDiscountPercent.Attributes.Add("onclick", "Select('" + e.Item.ItemIndex + "');");
                rntxtDiscountPrice.Attributes.Add("onclick", "Select('" + e.Item.ItemIndex + "');");
                rntxtGST.Attributes.Add("onclick", "Select('" + e.Item.ItemIndex + "');");
                rntxtUnitPrice.Attributes.Add("onclick", "Select('" + e.Item.ItemIndex + "');");
                rbtnPercent.Attributes.Add("onclick", "Select('" + e.Item.ItemIndex + "');");
                rbtnPrice.Attributes.Add("onclick", "Select('" + e.Item.ItemIndex + "');");

                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                    {
                        img.ImageUrl = dr["Image"].ToString();
                        txtProductDescription.Text = dr["ProductDescription"].ToString();
                        rntxtGST.Value = convertCurrencyToDouble(dr["GST"].ToString());
                        rntxtUnitPrice.Value = convertCurrencyToDouble(dr["UnitPrice"].ToString());

                        rbtnPrice.Checked = dr["DiscountType"].ToString() == "Pri" ? true : false;
                        rbtnPercent.Checked = dr["DiscountType"].ToString() == "Per" ? true : false;
                        rntxtQuantity.Value = Convert.ToDouble(dr["Quantity"].ToString());
                        rntxtDiscountPrice.Value = Convert.ToDouble(dr["DiscountPrice"].ToString());
                        rntxtDiscountPercent.Value = Convert.ToDouble(dr["DiscountPercent"].ToString());
                        if (dr["DiscountType"].ToString() == "Per")
                        {
                            divDiscountPercent.Style.Add("display", "block");
                            divDiscountPrice.Style.Add("display", "none");
                        }
                        else if (dr["DiscountType"].ToString() == "Pri")
                        {
                            divDiscountPercent.Style.Add("display", "none");
                            divDiscountPrice.Style.Add("display", "block");
                        }
                        rntxtTotalPrice.Value = Convert.ToDouble(dr["TotalPrice"].ToString());
                        sum += rntxtTotalPrice.Value;
                        break;
                    }
                }
            }
            else if (e.Item is GridFooterItem)
            {
                GridFooterItem footer = (GridFooterItem)e.Item;
                (footer["TotalPrice"].FindControl("rntxtTotalPriceFooter") as RadNumericTextBox).Value = sum;
            }
        }
        private double? getTotalPrice(double? unitprice, double? quantity, double? discountper, double? discountpri, double? gst)
        {
            double? total = 0;
            total = unitprice * quantity;
            total += total * gst / 100.0;
            if (discountper != null)
                total -= total * discountper / 100.0;
            else if (discountpri != null)
                total -= discountpri;
            if (total < 0) total = 0;
            return total;
        }
        private DataTable saveallvaluebeforeventfire(DataTable dt)
        {

            foreach (GridDataItem item in RadGrid1.Items)
            {

                TextBox txtProductDescription = (TextBox)item.FindControl("txtProductDescription");
                RadNumericTextBox rntxtGST = (RadNumericTextBox)item.FindControl("rntxtGST");
                System.Web.UI.WebControls.Image img = (System.Web.UI.WebControls.Image)item.FindControl("productImage");
                RadNumericTextBox rntxtUnitPrice = (RadNumericTextBox)item.FindControl("rntxtUnitPrice");
                RadNumericTextBox rntxtQuantity = (RadNumericTextBox)item.FindControl("rntxtQuantity");
                RadNumericTextBox rntxtDiscountPercent = (RadNumericTextBox)item.FindControl("rntxtDiscountPercent");
                RadNumericTextBox rntxtDiscountPrice = (RadNumericTextBox)item.FindControl("rntxtDiscountPrice");
                RadNumericTextBox rntxtTotalPrice = (RadNumericTextBox)item.FindControl("rntxtTotalPrice");
                RadButton rbtnPercent = (RadButton)item.FindControl("rbtnPercent");

                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                    {
                        dr["UnitPrice"] = rntxtUnitPrice.Value;
                        dr["GST"] = rntxtGST.Value;
                        dr["Quantity"] = rntxtQuantity.Value;
                        dr["DiscountType"] = rbtnPercent.Checked ? "Per" : "Pri";
                        dr["DiscountPrice"] = rbtnPercent.Checked ? 0 : rntxtDiscountPrice.Value;
                        dr["DiscountPercent"] = rbtnPercent.Checked ? rntxtDiscountPercent.Value : 0;
                        double? total = 0;
                        total = rbtnPercent.Checked ? getTotalPrice(rntxtUnitPrice.Value, rntxtQuantity.Value, rntxtDiscountPercent.Value, null, rntxtGST.Value) :
                            getTotalPrice(rntxtUnitPrice.Value, rntxtQuantity.Value, null, rntxtDiscountPrice.Value, rntxtGST.Value);
                        dr["Totalprice"] = total;

                        break;
                    }
                }
            }
            return dt;
        }
        private void setProductInfo()
        {
            GridDataItem item = RadGrid1.Items[Convert.ToInt32(hdntxtCurrentRow.Value)];
            DataTable dt = (DataTable)ViewState["Products"];

            //update all value change before event fire
            dt = saveallvaluebeforeventfire(dt);

            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                {
                    dr["Image"] = hdntxtProductImage.Value;
                    dr["ProductID"] = hdntxtProductID.Value;
                    dr["ProductName"] = hdntxtProductName.Value;
                    dr["ProductDescription"] = hdntxtProductDes.Value;
                    dr["GST"] = convertCurrencyToDouble(hdntxtProductGST.Value);
                    dr["UnitPrice"] = convertCurrencyToDouble(hdntxtProductUnitPrice.Value);

                    double? totalchange = 0;
                    totalchange = getTotalPrice(Convert.ToDouble(dr["UnitPrice"].ToString()), Convert.ToDouble(dr["Quantity"].ToString()), Convert.ToDouble(dr["DiscountPercent"].ToString()), Convert.ToDouble(dr["DiscountPrice"].ToString()), Convert.ToDouble(dr["GST"].ToString()));
                    dr["Totalprice"] = totalchange;
                    break;
                }
            }
            dt.AcceptChanges();
            ViewState["Products"] = dt;
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
        protected void btnAddProduct_ServerClick(object sender, EventArgs e)
        {
            //DataTable dt = GetAllRow();
            DataTable dt = (DataTable)ViewState["Products"];
            dt = AddRow(dt);
            ViewState["Products"] = dt;

            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }

        protected void btnCloseRadWin_ServerClick(object sender, EventArgs e)
        {
            if (hdntxtProductID.Value != "")
            {
                setProductInfo();
                hdntxtProductID.Value = "";
            }
        }

        protected void rntxtDiscountPercent_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Products"];
            RadNumericTextBox txt = sender as RadNumericTextBox;
            GridDataItem item = txt.Parent.Parent.Parent as GridDataItem;
            RadButton rbtnPercent = (RadButton)item.FindControl("rbtnPercent");
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                {
                    dr["DiscountType"] = rbtnPercent.Checked ? "Per" : "Pri";
                    dr["DiscountPercent"] = txt.Value;
                    break;
                }
            }
        }

        protected void rntxtDiscountPrice_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Products"];
            RadNumericTextBox txt = sender as RadNumericTextBox;
            GridDataItem item = txt.Parent.Parent.Parent as GridDataItem;
            RadButton rbtnPercent = (RadButton)item.FindControl("rbtnPercent");
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                {
                    dr["DiscountType"] = rbtnPercent.Checked ? "Per" : "Pri";
                    dr["DiscountPrice"] = txt.Value;
                    break;
                }
            }
        }
        private double? convertCurrencyToDouble(string str)
        {
            if (str != "")
                return Double.Parse(str, NumberStyles.AllowCurrencySymbol | NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands, new CultureInfo("en-US"));
            return 0;
        }
        protected void rntxtQuantity_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Products"];
            RadNumericTextBox txt = sender as RadNumericTextBox;
            GridDataItem item = txt.Parent.Parent as GridDataItem;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                {
                    dr["Quantity"] = txt.Value;
                    break;
                }
            }
        }
        protected void rntxtGST_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Products"];
            RadNumericTextBox txt = sender as RadNumericTextBox;
            GridDataItem item = txt.Parent.Parent.Parent as GridDataItem;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                {
                    dr["GST"] = txt.Value;
                    break;
                }
            }
        }

        protected void rntxtUnitPrice_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Products"];
            RadNumericTextBox txt = sender as RadNumericTextBox;
            GridDataItem item = txt.Parent.Parent as GridDataItem;
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                {
                    dr["UnitPrice"] = txt.Value;
                    break;
                }
            }
        }
        protected void rntxtTotalPrice_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ViewState["Products"];
            RadNumericTextBox txt = sender as RadNumericTextBox;
            GridDataItem item = txt.Parent.Parent as GridDataItem;

            RadButton rbtnPercent = (RadButton)item.FindControl("rbtnPercent");
            RadNumericTextBox rntxtUnitPrice = (RadNumericTextBox)item.FindControl("rntxtUnitPrice");
            RadNumericTextBox rntxtQuantity = (RadNumericTextBox)item.FindControl("rntxtQuantity");
            RadNumericTextBox rntxtDiscountPercent = (RadNumericTextBox)item.FindControl("rntxtDiscountPercent");
            RadNumericTextBox rntxtDiscountPrice = (RadNumericTextBox)item.FindControl("rntxtDiscountPrice");
            RadNumericTextBox rntxtGST = (RadNumericTextBox)item.FindControl("rntxtGST");
            
            foreach (DataRow dr in dt.Rows)
            {
                if (dr["ID"].ToString() == item.GetDataKeyValue("ID").ToString())
                {
                    double? totalprice = 0;
                    double? discountprice = 0;
                    totalprice = rntxtUnitPrice.Value * rntxtQuantity.Value;
                    totalprice = totalprice + totalprice * rntxtGST.Value / 100.0;
                    if (rbtnPercent.Checked)
                        discountprice = (totalprice * rntxtDiscountPercent.Value / 100.0);
                    else
                        discountprice = rntxtDiscountPrice.Value;
                    totalprice = totalprice - discountprice;
                    if (totalprice < 0) totalprice = 0;
                    dr["TotalPrice"] = totalprice;
                    break;
                }
            }
        }
    }
}